
(function ($) {
    "use strict";
    var input = $('.validate-input .inputcheck');

    $('.contact100-form-btn').on('click',function(){
        var check = checkdata(input);
        if(check){
           $.post('/admin/login/login/',  $('.contact100-form').serializeArray(), function(data) {
                if(data.code==200){
                    window.location.href='/admin/index/index/'
                }else{
                    toast(data.msg,3)
                }
        })
     
        }else{
         return false;   
        }    
    });

    function checkdata(input) {
        var check = true;

        for(var i=0; i<input.length; i++) {
            if(validate(input[i]) == false){
                showValidate(input[i]);
                check=false;
            }else{
                hideValidate(input[i]);
                // check=false;  
            }
        }

        return check;
    }

    function validate(input) {
        if($(input).attr('type') == 'code' || $(input).attr('name') == 'code') {
            if($(input).val().trim() == '') {
                return false;
            }else if($(input).val().trim() != $('.codes').val()) {
                $('.codevalue').attr('data-validate','输入的验证码不正确')
                return false;
            }
        }
        else {
            if($(input).val().trim() == ''){
                return false;
            }
        }
    }

    function showValidate(input) {
        var thisAlert = $(input).parent();
        $(thisAlert).addClass('alert-validate');
    }

    function hideValidate(input) {
        var thisAlert = $(input).parent();
        $(thisAlert).removeClass('alert-validate');
    }


    /* 
     * 弱提示 toast 
     */
    function toast(text,time) {
        var model = document.getElementById('toast-popup');
        if (model) {
            var content = document.getElementById('toast-content');
            content.innerText = text;
            model.style.display = 'block';
            cancelToast(time);
            return
        }
        var creatediv = document.createElement('div'); // 创建div
        creatediv.setAttribute('style', 'width: 280px; z-index:100; background: rgba(0,0,0,.5); border-radius: 8px; text-align: center; position: fixed; top: 50%; left: 50%; margin-top: -120px; margin-left: -226px;');
        creatediv.setAttribute('id','toast-popup'); // 添加ID
        var contentHtml = '<div id="toast-content" style="padding: 38px 40px; text-align: center; font-size: 16px; color: #fff;">'+text+'</div>'
        creatediv.innerHTML = contentHtml;
        document.body.appendChild(creatediv); // 将创建的div 加入 body
        cancelToast(time);
    }
    /* 
     * 弱提示关闭 默认2s
    */
    function cancelToast(time) {
        if(!time) {
            var time = 2; // 关闭时间默认在2s
        }
        setTimeout(function(){
            document.getElementById('toast-popup').style.display = 'none';  
        },time*1000)
    }

})(jQuery);